package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.ProductCategory;
import com.ywgt.system.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "商品分类模块",tags = "商品分类模块")
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    private List<ProductCategory> list=new ArrayList<>();

    @Autowired
    ProductCategoryService productCategoryService;
    @ApiOperation(value = "产品分类",notes = "产品分类树")
    @GetMapping("/categoryTree")
    public Result categoryTree(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize){
        List<ProductCategory> menuTree = productCategoryService.getMenuTree();
        List<ProductCategory> categories = menuTree.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        return Result.ok().data("rows", categories);
    }
    @ApiOperation(value = "获取所有父级分类",notes = "产品父级分类树")
    @GetMapping("/getParentCategoryTree")
    public Result getParentCategoryTree(){
        List<ProductCategory> productCategories=productCategoryService.getParentMenu();
        return Result.ok().data("row",productCategories);
    }

    @ApiOperation(value = "添加商品分类",notes ="添加商品分类" )
    @PostMapping("/add")
    public Result addCategory(@RequestBody ProductCategory productCategory){
        if (productCategoryService.save(productCategory)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "获取分类信息",notes ="根据id获取分类信息" )
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable("id") Long id){
        LambdaQueryWrapper<ProductCategory> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategory::getId,id);
        ProductCategory productCategory = productCategoryService.getOne(queryWrapper);
        return Result.ok().data("row",productCategory);
    }

    @ApiOperation(value = "更新商品分类",notes ="更新商品分类" )
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,@RequestBody ProductCategory productCategory){
        LambdaUpdateWrapper<ProductCategory> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProductCategory::getId,id);
        if (productCategoryService.update(productCategory,updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除商品分类",notes ="删除商品分类" )
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        productCategoryService.delete(id);
        return Result.ok();
    }

}

